﻿// Copyright (c) 2012-2013 fancidev
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;

namespace Util.Media
{
    /// <summary>
    /// Represents a frame in a multi-frame image.
    /// </summary>
    public class ImageFrame
    {
        private System.Drawing.Image image;
        private int delay;

        /// <summary>
        /// Creates an ImageFrame with the given image and delay.
        /// </summary>
        /// <param name="image">The image to display for the frame.</param>
        /// <param name="delay">The delay of the frame.</param>
        public ImageFrame(System.Drawing.Image image, int delay)
        {
            this.image = image;
            this.delay = delay;
        }

        /// <summary>
        /// Gets the image to display for the frame.
        /// </summary>
        public System.Drawing.Image Image { get { return image; } }

        /// <summary>
        /// Gets the delay in milliseconds before displaying the next frame.
        /// </summary>
        public int Delay { get { return delay; } }
    }

    /// <summary>
    /// Provides methods to encode an image to a stream.
    /// </summary>
    public abstract class ImageEncoder : IDisposable
    {
        /// <summary>
        /// Creates an <code>ImageEncoder</code>.
        /// </summary>
        protected ImageEncoder() { }

#if false
        /// <summary>
        /// Gets the number of frames in the image.
        /// </summary>
        public abstract int FrameCount { get; }
#endif

        /// <summary>
        /// Encodes the next frame in the image.
        /// </summary>
        /// <param name="frame">The frame to encode.</param>
        public abstract void EncodeFrame(ImageFrame frame);

        public void Dispose()
        {
            Dispose(true);
        }

        public virtual void Dispose(bool disposing) { }
    }

    /// <summary>
    /// Provides methods to decode an image from a stream.
    /// </summary>
    public abstract class ImageDecoder : IDisposable
    {
        /// <summary>
        /// Creates an <code>ImageDecoder</code>.
        /// </summary>
        protected ImageDecoder() { }

        /// <summary>
        /// Gets the number of frames in the image.
        /// </summary>
        public abstract int FrameCount { get; }

        /// <summary>
        /// Decodes the next frame in the image.
        /// </summary>
        /// <returns>The decoded frame.</returns>
        public abstract ImageFrame DecodeFrame();

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing) { }
    }
}
